<?php

// phpMyRealty 3
//
// File Name: locimport.php
// File Location : ./admin
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// IMPORT SCRIPT
define('PMR', 'true');
define('PMRADMIN', 'true');

include ( '.././config.php' );
include ( PATH . '/defaults.php' );

@set_time_limit(1800); 
ini_set('auto_detect_line_endings',true); 

// Title tag content
$title = '3-level Locations CSV Import';

// Template header
include ( PATH . '/admin/template/header.php' );

// If logged we can start the page output
if (adminAuth($session->fetch('adminlogin'), $session->fetch('adminpassword')))

 {

 // Navigation menu
 include ( PATH . '/admin/navigation.php' );

 // Make sure this administrator have access to this script
 adminPermissionsCheck('manage_settings', $session->fetch('adminlogin')) or error ('Critical Error', 'Incorrect privileges');

 if (isset($_FILES['file']['tmp_name'])) 

  {

   $in = file ($_FILES['file']['tmp_name']) or die ('<font color="red">CSV file can not be found</font> Exiting..');

   echo '<div align="left">';

   echo '<b><font color="green">CSV FILE UPLOADED SUCCESSFULLY</font></b><br /><br />';

   echo '<b><font color="green">PROCESSING...</font><br /></b>';

   foreach ($in as $key => $value)
    {

     $value = trim($value);

     // We use ; as a delimiter
     $csv = explode (';', $value);

     // 3 level locations

     $location1 = $csv[0];
     $location2 = $csv[1];
     $location3 = $csv[2];

     // 1ST LEVEL

     // Check if the 1st level location exists
     $r11 = $db->query('SELECT selector FROM ' . LOCATION1_TABLE . ' WHERE category = "' . $location1 . '"');
     $f11 = $db->fetcharray($r11);

     // If it is already there we just set the ID
     if ($db->numrows($r11) > 0)
      {
       $location1_ID = $f11['selector'];
      }
     // If we have no such location we add it
     else
      {

       $r_select = $db->query ('SELECT MAX(selector) AS maxselector FROM ' . LOCATION1_TABLE);
       $f_select = $db->fetcharray ($r_select);
       $selector=$f_select['maxselector'] + 1;

       $db->query('INSERT INTO ' . LOCATION1_TABLE . ' (selector, category) VALUES (' . $selector . ', "' . safehtml($csv[0]) . '")');
       $location1_ID = $selector;
       echo '<b><font color="green">NEW 1ST LEVEL LOCATION ADDED ' . safehtml($location1) . '(' . $location1_ID . ')</font></b><br />';
      }

     // 2ND LEVEL

     if ($location2 != '') {

     // Check if the 2nd level location exists with this 1st level location
     $r22 = $db->query('SELECT catsubsel FROM ' . LOCATION2_TABLE . ' WHERE subcategory = "' . $location2 . '" AND catsel = ' . $location1_ID . '');
     $f22 = $db->fetcharray($r22);

     // If it is already there we just set the ID
     if ($db->numrows($r22) > 0)
      {
       $location2_ID = $f22['catsubsel'];
      }
     // If we have no such location we add it
     else
      {
       if ($location2 != '') {
        $r_select = $db->query ('SELECT MAX(catsubsel) AS maxselector FROM ' . LOCATION2_TABLE);
        $f_select = $db->fetcharray ($r_select);
        $selector=$f_select['maxselector'] + 1;

        $db->query('INSERT INTO ' . LOCATION2_TABLE . ' (catsel, catsubsel, subcategory) VALUES ("' . $location1_ID . '", ' . $selector . ', "' . safehtml($csv[1]) . '")');
        $location2_ID = $selector;
        echo '<b><font color="green">NEW 2ND LEVEL LOCATION ADDED TO ' . $location1 . ', NAME - ' . safehtml($location2) . '(' . $location2_ID . ')</font></b><br />';
       }
      }
     }

     // 3RD LEVEL

     if ($location3 != '') {

     // Check if the 3rd level location exists with this 2nd level location
     $r33 = $db->query('SELECT catsubsubsel FROM ' . LOCATION3_TABLE . ' WHERE subsubcategory = "' . $location3 . '" AND catsel = ' . $location1_ID . ' AND catsubsel = ' . $location2_ID . '');
     $f33 = $db->fetcharray($r33);

     // If it is already there we just set the ID
     if ($db->numrows($r33) > 0)
      {
       $location3_ID = $f33['catsubsubsel'];
      }
     // If we have no such location we add it
     else
      {

       if ($location3 != '') {
        $r_select = $db->query ('SELECT MAX(catsubsubsel) AS maxselector FROM ' . LOCATION3_TABLE);
        $f_select = $db->fetcharray ($r_select);
        $selector=$f_select['maxselector'] + 1;

        $db->query('INSERT INTO ' . LOCATION3_TABLE . ' (catsel, catsubsel, catsubsubsel, subsubcategory) VALUES ("' . $location1_ID . '", "' . $location2_ID . '", ' . $selector . ', "' . safehtml($csv[2]) . '")');
        $location3_ID = $selector;
        echo '<b><font color="green">NEW 3RD LEVEL LOCATION ADDED TO ' . $location1 . '/' . $location2 . ', NAME - ' . safehtml($location3) . '(' . $location3_ID . ')</font></b><br />';
       }
      }
     }

    }

     echo '</div>';

  }

 else

  {

   echo table_header ('3-level Locations CSV Import Tool');

?>

<form action = "<?php echo URL;?>/admin/locimport.php" method="POST" enctype="multipart/form-data">

CSV File: <input type="file" name="file"><input type="submit" value="Start"><br />

</form>

<br /><br />
Notes:<br /> Use CSV text files only. Do not upload XLS or any other BINARY files. Please note that you MUST use a UTF-8 compliant text editor and file in order to process accent marks and other languages properly.<br /><br />

1. Do a complete database backup before using this tool!<br />
2. Delimit CSV values with ;<br />
3. The exact format for the CSV (if you need more options, please, contact PMR support to have that added):<br />

<br /><span style="font-size:10px;">Location1;Location2;Location3</span><br /><br />

<?php

   echo table_footer ();

  }

 }

else

 error ('Critical Error' , 'Please, login to access this script.');

// Template footer
include ( PATH . '/admin/template/footer.php' );

?>